package org.smr.ministore.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import org.blade.msg.ErrorCode;
import org.blade.utils.IdGenUtils;
import org.blade.utils.Pager;
import org.blade.service.base.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smr.ministore.dao.GoodsSpecificationGroupMapper;
import org.smr.ministore.dao.GoodsSpecificationGroupOrganMapper;

import org.smr.ministore.entities.GoodsSpecificationGroup;
import org.smr.ministore.entities.GoodsSpecificationGroupOrgan;

import org.smr.ministore.entities.vo.Assign2OrganVO;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.GoodsSpecificationGroupService;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("goodsSpecificationGroupServiceImpl")
public class GoodsSpecificationGroupServiceImpl extends AbstractService<Long, GoodsSpecificationGroup> implements GoodsSpecificationGroupService {


    private static Logger logger = LoggerFactory.getLogger(GoodsSpecificationGroupServiceImpl.class);


    @Autowired
    private GoodsSpecificationGroupMapper mapper;

    @Autowired
    private GoodsSpecificationGroupOrganMapper goodsSpecificationGroupOrganMapper;

    @Autowired
	public GoodsSpecificationGroupServiceImpl(GoodsSpecificationGroupMapper mapper) {
		super(mapper);
	}


	public int save(GoodsSpecificationGroup entity) {
		super.saveAttatch(entity);

		return mapper.insert(entity);
	}

	public int updateById(GoodsSpecificationGroup id) {
		super.modifyAttach(id);

		return mapper.updateById(id);
	}

    @Transactional(readOnly = true)
    public GoodsSpecificationGroup queryById(Long id) {
		return mapper.selectById(id);
	}

	public int deleteById(Long id) {
		return mapper.deleteById(id);
	}

    @Transactional(readOnly = true)
    public Pager<GoodsSpecificationGroup> paginate( GoodsSpecificationGroup entity, Long pageSize, Long pageNum) {

		Pager<GoodsSpecificationGroup> pager = new Pager( pageSize, pageNum );
		List list  = mapper.pagination(entity, pager);
		pager.setEntities(list);
		return pager;
    }
	
	public int batchSave(List<GoodsSpecificationGroup> entities) {
        this.batchSaveAttatch(entities);
		return mapper.batchInsert(entities);
	}


    public int batchModify(List<GoodsSpecificationGroup> entities) {
        this.batchModifyAttatch(entities);
        return mapper.batchUpdate(entities);
        }


	public int deleteByIdLogical(Long id) {
        return mapper.deleteByIdLogical(id);
    }




	public int assign(Assign2OrganVO assign2Organ) {
		GoodsSpecificationGroup goodsSpecification = new GoodsSpecificationGroup();
		List<GoodsSpecificationGroupOrgan> references = goodsSpecification.assignTo(assign2Organ);
		IdGenUtils.batchSaveAttach(references);
		return this.goodsSpecificationGroupOrganMapper.batchInsert(references);
	}

	@Transactional(readOnly = true)
	public GoodsSpecificationGroup queryByPrimaryKey(GoodsSpecificationGroup goodsSpecification) {
		return mapper.selectByPrimaryKey(goodsSpecification);
	}

	public int assign(Assign2OrgansVO assignee) {
        GoodsSpecificationGroup goodsSpecification = new GoodsSpecificationGroup();
		goodsSpecification.setCreateBy(assignee.getCreateBy());
		goodsSpecification.setUpdateBy(assignee.getUpdateBy());

		List<GoodsSpecificationGroupOrgan> goodsSpecificationOrganPersistence = this.goodsSpecificationGroupOrganMapper.selectBy(assignee);

		goodsSpecification.want2Assign(goodsSpecificationOrganPersistence, assignee);
		if (CollectionUtils.isEmpty(assignee.getAssignees())) {
			return ErrorCode.Persistence.NONE_EFFECT;
		}
		List<GoodsSpecificationGroupOrgan> goodsOrgans = goodsSpecification.assignTo(assignee);
		IdGenUtils.batchSaveAttach(goodsOrgans);
		return this.goodsSpecificationGroupOrganMapper.batchInsert(goodsOrgans);
	}


	public Long getId() {
		return IdGenUtils.getNextLongId();
	}
}
